/*
	Figure S3. Vote Outcome in Study 2
*/

use data_study_2.dta, clear


// Set omitted categories
	gen zero_race = 0
	label var zero_race "White"
	gen zero_sex = 0
	label var zero_sex "Male"
	gen zero_eco = 0
	label var zero_eco "Maintain investment in energy"
	gen zero_biden = 0
	label var zero_biden "Vote Share: 51%"
	gen zero_exp = 0
	label var zero_exp "Political newcomer"
	gen zero_dist = 0
	label var zero_dist "[63, 8, 13, 11, 5]"
	gen zero_racepol = 0
	label var zero_racepol "Not shown policy"
	
// Set regression variables
	loc reg_race "cand_black zero_race cand_asian cand_hispa"
	loc reg_sex "cand_female zero_sex"
	loc reg_exp "cand_exp_teach cand_exp_council cand_exp_lawyer cand_exp_business zero_exp"
	loc reg_biden "cand_biden_p59 cand_biden_p57 cand_biden_p55 cand_biden_p53 zero_biden"
	loc reg_distpop "cand_dist1 cand_dist2 cand_dist3 cand_dist4 cand_dist5 cand_dist6 zero_dist"
	loc reg_issues "cand_policy_abort1 cand_policy_abort2 cand_policy_tax1 cand_policy_tax2"
	loc reg_issues "`reg_issues' cand_policy_health1 cand_policy_health2 cand_policy_eco1 zero_eco"
	loc reg_affirm "cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 zero_racepol"
	loc reg_affirm2 "noracepolicy cand_policy_aa1 cand_policy_aa2 cand_policy_aa3 "
	
// Interactive Terms for Black Candidate X Affirmative Action
	gen noracepolicy = cand_policy_aa1 == 0 & cand_policy_aa2 == 0 & cand_policy_aa3 == 0	
	
	loc int ""
	foreach r in white black asian hispa {
		gen `r'_aa1 = cand_policy_aa1*cand_`r'
		gen `r'_aa2 = cand_policy_aa2*cand_`r'
		gen `r'_aa3 = cand_policy_aa3*cand_`r'
		gen `r'_aa0 = noracepolicy*cand_`r'
		
		loc lab = proper("`r'")
		if "`lab'" == "Hispa" loc lab = "Hispanic"
	
		label var `r'_aa0 "`lab' X No Position"
		label var `r'_aa1 "`lab' X Expand"
		label var `r'_aa2 "`lab' X Keep"
		label var `r'_aa3 "`lab' X End"
		
		loc int "`int' `r'_aa0 `r'_aa1 `r'_aa2 `r'_aa3"
	
	}
	// set reference
	replace white_aa0 = 0
	label var noracepolicy "Not shown position"
	
	gen out_fair_bwdiff = out_fair_black - out_fair_white
		
//============================================================================== Regress the things

		eststo clear
		reg out_vote `reg_distpop' `reg_race' `reg_affirm' `reg_issues' `reg_sex' cand_age `reg_exp' `reg_biden' , vce(cluster r_id)
		eststo pool_vote
		loc b_pool_vote = string(round(_b[_cons], 0.001), "%9.3f")
		
		reg out_vote `reg_sex' `reg_exp' `reg_biden' `reg_distpop' `reg_issues' `int' cand_age, vce(cluster r_id)	
		eststo vote
		loc b_vote = string(round(_b[_cons], 0.001), "%9.3f")
		
		// All attributes, pooled 
		coefplot (pool_vote, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
					|| , drop(_cons cand_age) omitted baselevels ms(c) msize(medsmall) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black)) nokey ///
			subtitle("{bf:(a) Pooled Model}", bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) note("`notes'", size(vsmall)) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(cand_black = "{bf: Race}" ///
					cand_female = "{bf: Gender}" ///
					cand_exp_teach = "{bf: Occupation}" ///
					cand_biden_p59 = "{bf: District Vote for Biden}" ///
					cand_dist1 = "{bf: District Racial % [W,B,A,H,O]}" ///
					cand_policy_abort1 = "{bf: Abortion}" ///
					cand_policy_tax1 = "{bf: Tax Policy}" ///
					cand_policy_health1 = "{bf: Healthcare}" ///
					cand_policy_eco1 = "{bf: Energy}" ///
					cand_policy_aa1 = "{bf: Affirmative Action}" ///
					, labsize(vsmall)) note("Mean, Baseline Categories = `b_pool_vote'", size(vsmall)) ///
					saving(fig1.gph, replace)
	
		// Effect of candidate attributes on main outcomes, interacted
		coefplot (vote, label() mc(gs4) ciopts(color(gs4) lw(med))) ///
				 || , drop(_cons `reg_sex' `reg_issues' cand_age `reg_exp' `reg_biden' `reg_distpop') ///
				omitted baselevels ms(c) msize(med) ///
				ylabel(,labsize(vsmall)) ///
			xline(0, lc(black)) nokey ///
			subtitle("{bf:(b) Interacted Model}", bcolor(white) color(black) size(vsmall)) ///
			byopts(row(1) t1title("{bf:`title'}", size(small))) ///
			xtitle("Effects of Candidate Attributes (Scale 0 to 1)", size(vsmall)) ///
			xlabel(-0.2(0.1)0.2,labsize(small)) norecycle ///
			headings(white_aa0 = "{bf: White X Affirmative Action}" ///
					black_aa0 = "{bf: Black X Affirmative Action}" ///
					asian_aa0 = "{bf: Asian X Affirmative Action}" ///
					hispa_aa0 = "{bf: Hispanic X Affirmative Action}" ///
					, labsize(vsmall)) note("Mean, Baseline Categories = `b_vote'", size(vsmall)) ///
					saving(fig2.gph, replace)
					
		graph combine fig1.gph fig2.gph 

		graph display, xsize(4.5) ysize(3.4) margins(vsmall)	
		graph export figure_s3.png, as(png) replace
		